"""

@author: snh
@file: 13.剪刀石头布.py
@time: 2024/8/23 14:32
"""
import random

"""
可以创建游戏对象，两个游戏对象就可以加入到房间
同一个房间内的两个游戏对象可以进行游戏
每次两个游戏对象按任意键随机出剪刀石头布，三局两胜
输出每局结果及最终结果

玩家：
    属性：姓名，房间号
    方法：战斗
    
房间：
    属性：房间号，房间成员
    方法：加入玩家
    
1 石头
2 剪子
3 布 
"""


class Player:
    def __init__(self, name, room = None):
        self.name = name
        self.room = room

    def fight(self, player1, player2):
        nums = [1, 2, 3]
        results = ("剪刀", "石头", "布")
        score = 0
        for i in nums:
            ran1 = random.randint(1, 3)
            ran2 = random.randint(1, 3)
            if (ran1 - ran2 == -1) or (ran1 - ran2 == 2):
                print(
                    f"第{i}局：{player1}出的是{results[ran1 - 1]},{player2}出的是{results[ran2 - 1]}，第{i}局获胜的是{player2}")
                score -= 1
            elif (ran1 - ran2 == 1) or (ran1 - ran2 == -2):
                print(
                    f"第{i}局：{player1}出的是{results[ran1 - 1]},{player2}出的是{results[ran2 - 1]}，第{i}局获胜的是{player1}")
                score += 1
            elif ran1 - ran2 == 0:
                print(f"第{i}局：{player1}出的是{results[ran1 - 1]},{player2}出的是{results[ran2 - 1]}，第{i}局平手")
        if score > 0:
            print(f"经过三局的比拼，{player1}获胜")
        elif score < 0:
            print(f"经过三局的比拼，{player2}获胜")
        elif score == 0:
            print(f"经过三局的比拼，两人平手，未分出胜负")



class Room:
    def __init__(self, room_id):
        self.room_id = room_id
        self.room_members = []

    def add_player(self, pname):
        if pname in playInfo:
            if roomInfo[r_num]["player1"] == "":
                roomInfo[r_num]["player1"] = pname
                print(f"{pname}加入房间成功")
            else:
                roomInfo[r_num]["player2"] = pname
                print(f"{pname}加入房间成功")
        else:
            print("该用户不存在，前往创建用户")
            create_player()


"""
无限输入
1.创建房间
2.创建角色
3.将角色加入房间
4.选择房间战斗
5.退出系统
"""


def create_player():
    p_name = input("请输入要新建的用户名：")
    if p_name in playInfo:
        print("该用户名已存在")
    else:
        player = Player(p_name)
        playInfo[player.name] = ""
        print("角色创建成功")


roomInfo = {}
playInfo = {}

while(True):
    print("欢迎来到石头剪刀布！！！")
    print("1.创建房间")
    print("2.创建角色")
    print("3.将角色加入房间")
    print("4.选择房间战斗")
    print("5.退出系统")
    choice = input("请输入您的选择：")
    if choice == "1":
        r_num = str(input("请输入要新建的房间号："))
        if r_num in roomInfo:
            print(roomInfo)
            print("该房间已存在")
        else:
            room = Room(r_num)
            roomInfo[room.room_id] = {"player1": "", "player2": ""}
            print(f"{r_num}号房间已创建成功")
        continue
    elif choice == "2":
        create_player()
        continue
    elif choice == "3":
        r_num = input("请输入您要加入的房间")
        if r_num in roomInfo and (roomInfo[r_num]["player1"] == "" or roomInfo[r_num]["player2"] == ""):
            p_name = input("请输入您的用户名：")
            room.add_player(p_name)
        else:
            print("该房间不存在，或房间人数已满！！！")
    elif choice == "4":
        print("准备开始战斗！！！")
        r_num = input("请输入您所在的房间号：")
        if roomInfo[r_num]["player1"] != "" and roomInfo[r_num]["player2"] != "":
            print("战斗开始！！！")
            p1 = Player(roomInfo[r_num]["player1"])
            p2 = Player(roomInfo[r_num]["player2"])
            p1.fight(p1.name, p2.name)
        else:
            print("您所在房间，人数不足两人，无法开始战斗！！！")
    elif choice == "5":
        print("退出系统！！！")
        break
